package gui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import logic.objects.Node;
import logic.objects.Problem;

public class CoordinateTable extends JTable {
	private String[] columns = { "x", "y" };
	private ArrayList<int[]> list;
	private Problem problem;

	public CoordinateTable(Problem problem) {
		this.problem = problem;
		list = new ArrayList<int[]>();
		for (Node node : problem.getNodes()) {
			add(node.getX(), node.getY());
		}
		applyModel();
	}

	public void add(int x, int y) {
		int[] array = { x, y };
		list.add(array);
		applyModel();
	}

	public void remove(Integer[] index) {
		list.remove(index);
		applyModel();
	}

	public void removeAll(int[] index) {
		ArrayList<int[]> remove = new ArrayList<int[]>();
		for (int id : index)
			remove.add(list.get(id));
		list.removeAll(remove);
		applyModel();
	}

	public String[][] getData() {
		String[][] array = new String[list.size()][2];
		for (int i = 0; i < list.size(); i++) {
			array[i][0] = list.get(i)[0] + "";
			array[i][1] = list.get(i)[1] + "";

		}
		return array;
	}

	public String[] getColumns() {
		return columns;
	}

	public void applyModel() {
		this.setModel(new DefaultTableModel(getData(), getColumns()));
	}

	public void applyChanges() {
		problem.deleteAllNodes();
		for (int[] node : list) {
			problem.addNode(node[0], node[1]);
		}
		problem.applyConnections();
		problem.reset();
	}
}
